﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.DAL;
using MINERP.DAL.InfoConfig;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.InfoConfig;

namespace MINERP.BLL.InfoConfig
{
    public class ModifyProductPriceBll
    {
        public List<string> GetAllAresa()
        {
            List<string> rtnList = new List<string>();
            rtnList.Add("不限制");
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> listShop = db.Shop.ToList();
                rtnList = listShop.Select(p => p.Area).Distinct().ToList();
                rtnList.Add("不限制");

            }
            return rtnList;
        }
        public List<int> GetAllLevel()
        {
            List<int> rtnList = new List<int>();

            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> listShop = db.Shop.ToList();
                rtnList = listShop.Select(p => (int)p.CreditLevel).Distinct().ToList();
                rtnList.Add(-1);

            }
            return rtnList;
        }
        public List<string> GetAllsaleman()
        {
            List<string> rtnList = new List<string>();
            rtnList.Add("不限制");
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> listShop = db.Shop.ToList();
                rtnList = listShop.Select(p => p.Salesman).Distinct().ToList();
                rtnList.Add("不限制");

            }
            return rtnList;
        }
        public List<tb_CostType_info> GetAllCostType()
        {
            List<tb_CostType_info> rtnList = new List<tb_CostType_info>();
            rtnList.Add(new tb_CostType_info() { CostTypeId = 0, TypeName = "不限制" });
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<tb_CostType> ListCost = db.tb_CostType.ToList();
                foreach (tb_CostType type in ListCost)
                {
                    tb_CostType_info info = new tb_CostType_info();
                    info.CostTypeId = type.CostTypeId;
                    info.TypeName = type.TypeName;
                    rtnList.Add(info);
                }
                return rtnList;
            }
        }

        public List<PaperType_Info> GetAllPaperType()
        {
            List<PaperType_Info> rtnList = new List<PaperType_Info>();
            rtnList.Add(new PaperType_Info() { PaperTypeID = 0, Name = "不限制" });
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<paperType> listPaperType = db.paperType.ToList();
                foreach (paperType type in listPaperType)
                {
                    PaperType_Info info = new PaperType_Info();
                    info.PaperTypeID = type.PaperTypeID;
                    info.Name = type.Name;
                    rtnList.Add(info);
                }
            }
            return rtnList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inPaperInfo">纸型</param>
        /// <param name="inChangType">0更改成统一价，1变化统一价格</param>
        /// <param name="inAddType">商户没有此纸型0不添加1添加</param>
        /// <param name="inValue">改变的值</param>
        /// <param name="inArea">地区</param>
        /// <param name="insaleMan">销售员</param>
        /// <param name="inShopLevel">商户等级</param>
        /// <param name="inCostType">计价类型</param>
        /// <returns></returns>
        public int updatePrintPrice(Shop_Paper_Price_Info inPaperInfo, int inChangType, int inAddType, string inArea, string insaleMan, int inShopLevel, tb_CostType_info inCostType)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = db.Shop.Where(p => p.ShopId == 0).ToList();
                if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.ToList();

                }
                else if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan).ToList();
                }
                else if ((!inArea.Equals("不限制")) && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan && p.Area == inArea).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Salesman.Trim() == insaleMan).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea && p.Salesman.Trim() == insaleMan).ToList();
                }
                if (!inPaperInfo.isShopPaperPrice && !inPaperInfo.isShopPaperPrice2)
                {
                    foreach (Shop shop in shopList)
                    {
                        if (!shop.Shop_Paper_Price.IsLoaded)
                        {
                            shop.Shop_Paper_Price.Load();
                        }
                        Shop_Paper_Price shopPaperPrice = shop.Shop_Paper_Price.FirstOrDefault(p => p.PaperTypeId == inPaperInfo.PaperType.PaperTypeID);
                        if (shopPaperPrice != null)
                        {
                            db.DeleteObject(shopPaperPrice);
                        }
                    }
                    db.SaveChanges();
                }
                else
                {
                    foreach (Shop shop in shopList)
                    {
                        if (!shop.Shop_Paper_Price.IsLoaded)
                        {
                            shop.Shop_Paper_Price.Load();
                        }
                        Shop_Paper_Price shopPaperPrice = null;


                        shopPaperPrice = shop.Shop_Paper_Price.FirstOrDefault(p => p.PaperTypeId == inPaperInfo.PaperType.PaperTypeID);



                        if (shopPaperPrice == null && inAddType == 1)//如果商户没有这个纸型就添加
                        {
                            //shop.Shop_Paper_Price.Add();
                            Shop_Paper_Price spp = new Shop_Paper_Price();
                            spp.Shop = shop;
                            spp.paperType =
                                db.paperType.FirstOrDefault(p => p.PaperTypeID == inPaperInfo.PaperType.PaperTypeID);
                            spp.UnitPrice = inPaperInfo.UnitPrice;
                            shop.Shop_Paper_Price.Add(spp);
                        }
                        else if (shopPaperPrice == null && inAddType == 2)
                        {
                            continue;//只添加不更新

                        }
                        else if (shopPaperPrice != null && inChangType == 0)
                        {
                            shopPaperPrice.UnitPrice = inPaperInfo.UnitPrice;
                        }
                        else if (shopPaperPrice != null && inChangType == 1)
                        {
                            shopPaperPrice.UnitPrice = shopPaperPrice.UnitPrice + inPaperInfo.UnitPrice;
                        }



                    }
                    db.SaveChanges();
                }
            }
            return 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inProduct">产品</param>
        /// <param name="inChangType">0更改成统一价，1变化统一价格</param>
        /// <param name="inAddType">商户没有此纸型0不添加1添加</param>
        /// <param name="inValue">改变的值</param>
        /// <param name="inArea">地区</param>
        /// <param name="insaleMan">销售员</param>
        /// <param name="inShopLevel">商户等级</param>
        /// <param name="inCostType">计价类型</param>
        /// <returns></returns>
        public int UpdateShopProductPrice(ProductDDO inProduct, int inChangType, int inAddType, string inArea, string insaleMan, int inShopLevel, tb_CostType_info inCostType)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = null;
                if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.ToList();

                }
                else if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan).ToList();
                }
                else if ((!inArea.Equals("不限制")) && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan && p.Area == inArea).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Salesman.Trim() == insaleMan).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea && p.Salesman.Trim() == insaleMan).ToList();
                }
                if (!inProduct.IsShopProduct && !inProduct.IsShopProduct2)//删除产品
                {
                    foreach (Shop shop in shopList)
                    {
                        if (!shop.Shop_Product_Price.IsLoaded)
                        {
                            shop.Shop_Product_Price.Load();
                        }
                        Shop_Product_Price shopProductPrice = shop.Shop_Product_Price.FirstOrDefault(p => p.ProductId == inProduct.ProuctId);
                        if (shopProductPrice != null)
                        {
                            db.DeleteObject(shopProductPrice);
                        }
                        shop.ProductVersion += 0.1;
                    }
                    db.SaveChanges();
                }
                else
                {
                    foreach (Shop shop in shopList)
                    {
                        if (!shop.Shop_Product_Price.IsLoaded)
                        {
                            shop.Shop_Product_Price.Load();
                        }
                        Shop_Product_Price shopProductPrice = null;
                        if (inCostType.TypeName == "不限制")
                        {
                            shopProductPrice = shop.Shop_Product_Price.FirstOrDefault(p => p.ProductId == inProduct.ProuctId);
                        }
                        else
                        {
                            shopProductPrice = shop.Shop_Product_Price.FirstOrDefault(p => p.ProductId == inProduct.ProuctId && p.tb_CostType.CostTypeId == inCostType.CostTypeId);
                            if (shopProductPrice == null)
                            {
                                continue;
                            }
                        }


                        if (shopProductPrice == null && inAddType == 1)//如果商户没有这个产品就添加
                        {
                            Shop_Product_Price spp = new Shop_Product_Price();
                            Tb_Product product = db.Tb_Product.FirstOrDefault(p => p.ProuctId == inProduct.ProuctId);
                            tb_CostType cost = db.tb_CostType.FirstOrDefault(p => p.TypeName == inProduct.CostTypeName);
                            if (cost == null)
                            {
                                db.tb_CostType.FirstOrDefault();
                            }

                            spp.Shop = shop;
                            spp.Tb_Product = product;
                            spp.tb_CostType = cost;
                            spp.UnitPrice = inProduct.UnitPrice;
                            spp.AppendPrice = inProduct.AppendPrice;
                            spp.SinglePrice = inProduct.SinglePrice;
                            db.AddToShop_Product_Price(spp);
                        }
                        else if (shopProductPrice != null && inAddType == 2)
                        {
                            continue;//只添加不更新
                        }
                        else if (shopProductPrice != null && inChangType == 0)
                        {

                            shopProductPrice.UnitPrice = inProduct.UnitPrice;
                            shopProductPrice.AppendPrice = inProduct.AppendPrice;
                            shopProductPrice.SinglePrice = inProduct.SinglePrice;
                        }
                        else if (shopProductPrice != null && inChangType == 1)
                        {
                            shopProductPrice.UnitPrice += inProduct.UnitPrice;
                            shopProductPrice.AppendPrice += inProduct.AppendPrice;
                            shopProductPrice.SinglePrice += inProduct.SinglePrice;
                        }
                        else
                        {
                            continue;
                        }
                        shop.ProductVersion += 0.1;

                    }
                    db.SaveChanges();

                }
            }
            return 0;
        }
        /// <summary>
        ///批量修改公共收费项目 
        /// </summary>
        /// <param name="inCommItem">公共收费项目</param>
        /// <param name="inChangType">0更改成统一价，1变化统一价格</param>
        /// <param name="inAddType">商户没有此纸型0不添加1添加</param>
        /// <param name="inValue">改变的值</param>
        /// <param name="inArea">地区</param>
        /// <param name="insaleMan">销售员</param>
        /// <param name="inShopLevel">商户等级</param>
        /// <param name="inCostType">计价类型</param>
        public int UpdateCommPrice(Shop_CommonChargeItem_Price_Info inCommItem, int inChangType, int inAddType, string inArea, string insaleMan, int inShopLevel, tb_CostType_info inCostType)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = null;
                if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.ToList();

                }
                else if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan).ToList();
                }
                else if ((!inArea.Equals("不限制")) && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan && p.Area == inArea).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Salesman.Trim() == insaleMan).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea && p.Salesman.Trim() == insaleMan).ToList();
                }
                if (!inCommItem.isShopCommomItem && !inCommItem.isShopCommomItem2)//删除模式
                {
                    foreach (Shop shop in shopList)
                    {

                        if (!shop.Shop_CommonChargeItem_Price.IsLoaded)
                        {
                            shop.Shop_CommonChargeItem_Price.Load();
                        }
                        Shop_CommonChargeItem_Price shopCommItemPrice = shop.Shop_CommonChargeItem_Price.FirstOrDefault(
                                                         p => p.CommonChargeItemId == inCommItem.CCII.CommonChargeItemId);
                        if (shopCommItemPrice != null)
                        {
                            db.DeleteObject(shopCommItemPrice);
                        }

                    }
                    db.SaveChanges();
                }
                else
                {
                    foreach (Shop shop in shopList)
                    {

                        if (!shop.Shop_CommonChargeItem_Price.IsLoaded)
                        {
                            shop.Shop_CommonChargeItem_Price.Load();
                        }
                        Shop_CommonChargeItem_Price shopCommItemPrice = null;
                        if (inCostType.TypeName == "不限制")
                        {
                            shopCommItemPrice = shop.Shop_CommonChargeItem_Price.FirstOrDefault(
                                 p => p.CommonChargeItemId == inCommItem.CCII.CommonChargeItemId);

                        }
                        else
                        {
                            shopCommItemPrice = shop.Shop_CommonChargeItem_Price.FirstOrDefault(
                                 p => p.CommonChargeItemId == inCommItem.CCII.CommonChargeItemId && p.CostTypeId == inCostType.CostTypeId);
                            if (shopCommItemPrice == null)
                            {
                                continue;//条件不符合跳出
                            }
                        }
                        if (shopCommItemPrice == null && inAddType == 1)//如果商户没有这个产品就添加
                        {

                            Shop_CommonChargeItem_Price sccip = new Shop_CommonChargeItem_Price();
                            CommonChargeItem commonChargeItem = db.CommonChargeItem.FirstOrDefault(p => p.CommonChargeItemId == inCommItem.CCII.CommonChargeItemId);
                            // CommonChargeItem item = db.CommonChargeItem.FirstOrDefault(p => p.TypeName == inCommItem.CCII.CommonChargeItemId.CostTypeName);
                            if (commonChargeItem == null)
                            {
                                db.CommonChargeItem.FirstOrDefault();
                            }

                            sccip.AreaPrice = inCommItem.AreaPrice;
                            sccip.Shop = shop;
                            sccip.SinglePrice = inCommItem.SinglePrice;
                            sccip.UnitPrice = inCommItem.UnitPrice;
                            sccip.CommonChargeItem = commonChargeItem;
                            sccip.CostTypeId = inCommItem.CCII.CostTypeId;
                            db.AddToShop_CommonChargeItem_Price(sccip);

                        }
                        else if (shopCommItemPrice != null && inAddType == 2)
                        {
                            continue;//只添加不更新
                        }
                        else if (shopCommItemPrice != null && inChangType == 0)
                        {
                            // shopCommItemPrice.UnitPrice = inValue;
                            shopCommItemPrice.SinglePrice = inCommItem.SinglePrice;
                            shopCommItemPrice.UnitPrice = inCommItem.UnitPrice;
                            shopCommItemPrice.AreaPrice = inCommItem.AreaPrice;
                            shopCommItemPrice.CostTypeId = inCommItem.CostType.CostTypeId;
                        }
                        else if (shopCommItemPrice != null && inChangType == 1)
                        {
                            shopCommItemPrice.SinglePrice += inCommItem.SinglePrice;
                            shopCommItemPrice.UnitPrice += inCommItem.UnitPrice;
                            shopCommItemPrice.AreaPrice += inCommItem.AreaPrice;
                            shopCommItemPrice.CostTypeId = inCommItem.CostType.CostTypeId;
                        }
                        else
                        {
                            continue;
                        }



                    }
                }
                db.SaveChanges();
            }
            return 0;
        }

        /// <summary>
        /// 批量删除商户的纸型价格表
        /// </summary>
        /// <param name="inPaperInfo">纸型</param>
        /// <param name="inChangType">修改模式</param>
        /// <param name="inAddType">添加模式</param>
        /// <param name="inArea">地区条件</param>
        /// <param name="insaleMan">业务员条件</param>
        /// <param name="inShopLevel">商户等级</param>
        /// <returns></returns>
        public int DelPrintPrice(Shop_Paper_Price_Info inPaperInfo, int inChangType, int inAddType, string inArea, string insaleMan, int inShopLevel)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = db.Shop.Where(p => p.ShopId == 0).ToList();
                if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.ToList();

                }
                else if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan).ToList();
                }
                else if ((!inArea.Equals("不限制")) && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan && p.Area == inArea).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Salesman.Trim() == insaleMan).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea && p.Salesman.Trim() == insaleMan).ToList();
                }

                foreach (Shop shop in shopList)
                {
                    if (!shop.Shop_Paper_Price.IsLoaded)
                    {
                        shop.Shop_Paper_Price.Load();
                    }
                    Shop_Paper_Price shopPaperPrice = null;


                    shopPaperPrice = shop.Shop_Paper_Price.FirstOrDefault(p => p.PaperTypeId == inPaperInfo.PaperType.PaperTypeID);



                    if (shopPaperPrice != null)
                    {
                        db.DeleteObject(shopPaperPrice);
                    }



                }
                db.SaveChanges();
            }
            return 0;
        }
        /// <summary>
        /// 批量删除商户的公共收费项目
        /// </summary>
        /// <param name="inCommItem"></param>
        /// <param name="inAddType"></param>
        /// <param name="inArea"></param>
        /// <param name="insaleMan"></param>
        /// <param name="inShopLevel"></param>
        /// <returns></returns>
        public int DelCommPrice(Shop_CommonChargeItem_Price_Info inCommItem, int inAddType, string inArea, string insaleMan, int inShopLevel)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<Shop> shopList = null;
                if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.ToList();

                }
                else if (inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan).ToList();
                }
                else if ((!inArea.Equals("不限制")) && !insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Salesman.Trim() == insaleMan && p.Area == inArea).ToList();
                }
                else if (inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Salesman.Trim() == insaleMan).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && insaleMan.Equals("不限制") && inShopLevel != -1)
                {
                    shopList = db.Shop.Where(p => p.CreditLevel == inShopLevel && p.Area == inArea).ToList();
                }
                else if (!inArea.Equals("不限制") && !insaleMan.Equals("不限制") && inShopLevel == -1)
                {
                    shopList = db.Shop.Where(p => p.Area == inArea && p.Salesman.Trim() == insaleMan).ToList();
                }

                foreach (Shop shop in shopList)
                {
                    if (!shop.Shop_CommonChargeItem_Price.IsLoaded)
                    {
                        shop.Shop_CommonChargeItem_Price.Load();
                    }
                    Shop_CommonChargeItem_Price scip = shop.Shop_CommonChargeItem_Price.FirstOrDefault(
                            p => p.CommonChargeItemId == inCommItem.CCII.CommonChargeItemId);
                    if (scip != null)
                    {
                        db.DeleteObject(scip);
                    }



                }
                db.SaveChanges();
            }
            return 0;
        }
        /// <summary>
        /// 得到所有商户列表
        /// </summary>
        /// <returns></returns>
        public List<Shop_New_Info> GetAllShopList()
        {
            List<Shop_New_Info> rtnList = new List<Shop_New_Info>();
            List<Shop> shopList = new List<Shop>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                shopList = db.Shop.Where(sh => sh.Status == 1).ToList();
                rtnList = ConvertShop_New_InfoFromShopEntity(shopList, db);
            }

            return rtnList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inModelshop"></param>
        /// <param name="inStandshop"></param>
        /// <param name="ModefyConent">更新内容-产品，输出，附加收费</param>
        /// <param name="ModefyType">更新方式-收费方式，价格，修改现有</param>
        /// <returns></returns>
        public int ModefyShopProduct(int inModelshopID, int inModefyshopID, int[] ModefyConent, int[] ModefyType)
        {
            int rtnInt = 0;
            if (inModelshopID == inModefyshopID)
            {
                rtnInt = 2;//原商户
            }
            bool isModefyPaperPrice = Convert.ToBoolean(ModefyConent[1]);
            bool isModefyProductPrice = Convert.ToBoolean(ModefyConent[0]);
            bool isModefyCommonItemPrice = Convert.ToBoolean(ModefyConent[2]);

            bool isModefyCostType = Convert.ToBoolean(ModefyType[0]);
            bool isModefyPrice = Convert.ToBoolean(ModefyType[1]);
            bool isModefyExist = Convert.ToBoolean(ModefyType[2]);
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                Shop Modelshop = db.Shop.FirstOrDefault(P => P.ShopId == inModelshopID);
                Shop ModefyShop = db.Shop.FirstOrDefault(P => P.ShopId == inModefyshopID);
                if (isModefyPaperPrice)//x修改纸型价格定义
                {
                    #region//修改纸型价格
                    List<Shop_Paper_Price> modelShopPaperPrice = db.Shop_Paper_Price.Where(p => p.Shop.ShopId == inModelshopID).ToList();
                    List<Shop_Paper_Price> modefyShopPaperPrice = db.Shop_Paper_Price.Where(p => p.Shop.ShopId == inModefyshopID).ToList();
                    modelShopPaperPrice.ForEach(p => p.paperTypeReference.Load());
                    modefyShopPaperPrice.ForEach(p => p.paperTypeReference.Load());
                    foreach (Shop_Paper_Price spp in modelShopPaperPrice)
                    {
                        paperType ptype = db.paperType.FirstOrDefault(p => p.PaperTypeID == spp.PaperTypeId);
                        Shop_Paper_Price modefyExistPaperPrice = modefyShopPaperPrice.FirstOrDefault(p => p.PaperTypeId == spp.PaperTypeId);
                        if (modefyExistPaperPrice != null)
                        {
                            if (isModefyExist)//是否修改现有的
                            {
                                modefyExistPaperPrice.UnitPrice = spp.UnitPrice;
                            }
                        }
                        else
                        {//为商户新建纸型价格
                            Shop_Paper_Price addPaperPrice = new Shop_Paper_Price();
                            addPaperPrice.paperType = ptype;
                            addPaperPrice.Shop = ModefyShop;
                            addPaperPrice.UnitPrice = spp.UnitPrice;
                            db.AddToShop_Paper_Price(addPaperPrice);
                        }


                    }
                #endregion

                }

                if (isModefyProductPrice)//修改产品价格
                {
                    #region//修改产品价格
                    List<Shop_Product_Price> ModelShopProductPrice = db.Shop_Product_Price.Where(p => p.Shop.ShopId == inModelshopID).ToList();
                    List<Shop_Product_Price> ModefyShopProductPrice = db.Shop_Product_Price.Where(p => p.Shop.ShopId == inModefyshopID).ToList();
                    ModelShopProductPrice.ForEach(p => p.tb_CostTypeReference.Load());
                    ModelShopProductPrice.ForEach(p => p.Tb_ProductReference.Load());
                    ModefyShopProductPrice.ForEach(p => p.tb_CostTypeReference.Load());
                    ModefyShopProductPrice.ForEach(p => p.Tb_ProductReference.Load());
                    foreach (Shop_Product_Price spp in ModelShopProductPrice)
                    {
                        Tb_Product product = spp.Tb_Product;
                        Shop_Product_Price modefySpp = ModefyShopProductPrice.FirstOrDefault(p => p.Tb_Product.ProuctId == product.ProuctId);
                        if (modefySpp != null)
                        {
                            if (isModefyExist)//修改现有产品
                            {
                                if (isModefyCostType)
                                {
                                    modefySpp.tb_CostType = spp.tb_CostType;
                                }
                                if (isModefyPrice)
                                {
                                    modefySpp.UnitPrice = spp.UnitPrice;
                                    modefySpp.SinglePrice = spp.SinglePrice;
                                    modefySpp.AppendPrice = spp.AppendPrice;

                                }

                            }
                            
                        }
                        else//为商户新加产品计价
                        {
                            Shop_Product_Price addspp = new Shop_Product_Price();
                            addspp.AppendPrice = spp.AppendPrice;
                            addspp.Shop = ModefyShop;
                            addspp.tb_CostType = spp.tb_CostType;
                            addspp.Tb_Product = product;
                            addspp.UnitPrice = spp.UnitPrice;
                            addspp.SinglePrice = spp.SinglePrice;
                            spp.usedTimes = 0;
                            db.AddToShop_Product_Price(addspp);

                        }
                    }

                    #endregion

                }
                if (isModefyCommonItemPrice)
                {//修改收费项目
                    List<Shop_CommonChargeItem_Price> ModelShopCommomItemPrice = db.Shop_CommonChargeItem_Price.Where(p => p.Shop.ShopId == inModelshopID).ToList();
                    List<Shop_CommonChargeItem_Price> ModefyShopCommomItemPrice = db.Shop_CommonChargeItem_Price.Where(p => p.Shop.ShopId == inModefyshopID).ToList();
                    ModelShopCommomItemPrice.ForEach(p => p.CommonChargeItemReference.Load());
                    foreach (Shop_CommonChargeItem_Price sccp in ModelShopCommomItemPrice)
                    {
                        Shop_CommonChargeItem_Price modefyExistSccip = ModefyShopCommomItemPrice.FirstOrDefault(p => p.CommonChargeItemId == sccp.CommonChargeItemId);

                        if (modefyExistSccip != null)
                        {
                            if (isModefyExist)
                            {
                                if (isModefyCommonItemPrice)
                                {
                                    modefyExistSccip.CostTypeId = sccp.CostTypeId;
                                    modefyExistSccip.AreaPrice = sccp.AreaPrice;
                                    modefyExistSccip.SinglePrice = sccp.SinglePrice;
                                    modefyExistSccip.UnitPrice = sccp.UnitPrice;

                                }
                            }
                        }
                        else
                        {
                             sccp.CommonChargeItemReference.Load();
                            
                            Shop_CommonChargeItem_Price addSCCIP = new Shop_CommonChargeItem_Price();
                            addSCCIP.AreaPrice = sccp.AreaPrice;
                           
                            addSCCIP.CommonChargeItem = sccp.CommonChargeItem;
                            addSCCIP.CostTypeId = sccp.CostTypeId;
                            addSCCIP.Shop = ModefyShop;
                            addSCCIP.SinglePrice = sccp.SinglePrice;
                            addSCCIP.UnitPrice = sccp.UnitPrice;
                            db.AddToShop_CommonChargeItem_Price(addSCCIP);
 
                        }
                    }

 
                }
                db.SaveChanges();
            }
            return rtnInt;
        }
        #region help methods
        /// <summary>
        /// 从SHOPENTITY转到SHOP_new_Info
        /// </summary>
        /// <param name="shopList"></param>
        /// <returns></returns>
        private List<Shop_New_Info> ConvertShop_New_InfoFromShopEntity(List<Shop> shopList, newSampleYPDBEntities db)
        {
            List<Shop_New_Info> shopInfoList = new List<Shop_New_Info>();
            //shopList.ForEach(s => shopInfoList.Add(new Shop_New_Info()
            //{
            //    Addr = s.address,
            //    AreaName = s.Area,
            //    ContactPerson = s.shopKeeper,
            //    CreditLevel = s.CreditLevel ?? 0,
            //    Email = s.EmailAddr,
            //    MiniCode = s.mnemonicCode,
            //    IsPrintPrice = s.IsNeedPrintUPrice ?? false,
            //    MSN = s.MSN,
            //    MobilePhoneNum = s.Tel1,
            //    PaperFaceID = s.PaperFaceId ?? 0,
            //    PaperTypeID = s.PaperTypeId ?? 0,
            //    PhoneNum = s.Tel2,
            //    SalesMan = s.Salesman,
            //    Priority = s.Priority ?? 0,
            //    QQ = s.QQ,
            //    Remark = s.Remark,
            //    ShopID = s.ShopId,
            //    ShopName = s.shopName,
            //    Status = s.Status ?? 0,
            //    IsAssignByCustom = s.IsAssignByCustom,
            //    FtpLoginName=s.FtpLoginName,
            //    FtpLoginPWD=s.FtpLoginPWD,
            //    SHACODE=s.SHACODE,
            //    IsForceCheck=s.ForceCheck


            //}));
            foreach (Shop s in shopList)
            {
                s.shop_DistributionInfoReference.Load();
                shopInfoList.Add(new Shop_New_Info()
                {
                    Addr = s.address,
                    AreaName = s.Area,
                    ContactPerson = s.shopKeeper,
                    CreditLevel = s.CreditLevel ?? 0,
                    Email = s.EmailAddr,
                    MiniCode = s.mnemonicCode,
                    IsPrintPrice = s.IsNeedPrintUPrice ?? false,
                    MSN = s.MSN,
                    MobilePhoneNum = s.Tel1,
                    PaperFaceID = s.PaperFaceId ?? 0,
                    PaperTypeID = s.PaperTypeId ?? 0,
                    PhoneNum = s.Tel2,
                    SalesMan = s.Salesman,
                    Priority = s.Priority ?? 0,
                    QQ = s.QQ,
                    Remark = s.Remark,
                    ShopID = s.ShopId,
                    ShopName = s.shopName,
                    Status = s.Status ?? 0,
                    IsAssignByCustom = s.IsAssignByCustom,
                    FtpLoginName = s.FtpLoginName,
                    FtpLoginPWD = s.FtpLoginPWD,
                    SHACODE = s.SHACODE,
                    IsForceCheck = s.ForceCheck,
                    DistributionInfo = s.shop_DistributionInfo == null ? "" : s.shop_DistributionInfo.ConsigneeName,
                    LastPrintDate = s.LastPrintDate

                });
            }

            return shopInfoList;
        }




        #endregion
    }
}
